<?php
/**
 *
 */
namespace app\manage\controller\notice;

use app\manage\controller\Base;

class Handle extends Base
{
    private $params;
    public function __construct(){
        parent::__construct();
        // token验证
        if(false == Base::checkToken()){
            $this->error('token error!');
        }
        // 参数去token
        $params = $this -> request -> param();
        unset($params['token']);
        $this->params = $params;
    }

    public function add(){
        $params = $this->params;
        $result = $this->validate($params,'Notice');
        if(true!==$result){
            $this->error($result);
        }

        $data = [
            'title' => $params['title'],
            'content' => $params['content'],
            'admin_id' => UID,
        ];
        $model = model('notice');
        if($model->allowField(true)->save($data)){
            $this->success('发布成功','notice.show/index');
        }else{
            $this->error('发布失败');
        }
    }

    public function del(){
        $params = $this->params;
        
        $id = isset($params['id'])?$params['id']:0;
        if(!$id)
            $this->error('id错误');

        $model = model('notice');
        if($model->where('id',$id)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}